<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of blog
 *
 * @author vudp
 */
class feed_Component_Block_Form_Blog extends Phpfox_Component{
    public function process(){
       		Phpfox::isUser(true);
		$bIsEdit = false;
		$bCanEditPersonalData = true;
		if (($iEditId = $this->request()->getInt('id')))
		{
			$oBlog = Phpfox::getService('blog');

			$aRow = $oBlog->getBlogForEdit($iEditId);

			if ($aRow['is_approved'] != '1' && !Phpfox::getUserParam('blog.edit_user_blog'))
			{
				return Phpfox_Error::display(Phpfox::getPhrase('blog.unable_to_edit_this_blog'));
			}

			if (Phpfox::isModule('tag'))
			{
				$aTags = Phpfox::getService('tag')->getTagsById('blog', $aRow['blog_id']);
				if (isset($aTags[$aRow['blog_id']]))
				{
					$aRow['tag_list'] = '';
					foreach ($aTags[$aRow['blog_id']] as $aTag)
					{
						$aRow['tag_list'] .= ' ' . $aTag['tag_text'] . ',';
					}
					$aRow['tag_list'] = trim(trim($aRow['tag_list'], ','));
				}
			}

			(Phpfox::getUserId() == $aRow['user_id'] ? Phpfox::getUserParam('blog.edit_own_blog', true) : Phpfox::getUserParam('blog.edit_user_blog', true));
			if (Phpfox::getUserParam('blog.edit_user_blog') && Phpfox::getUserId() != $aRow['user_id'])
			{
				$bCanEditPersonalData = false;
			}

			$aCategories = Phpfox::getService('blog.category')->getCategoriesById($aRow['blog_id']);
			$sCategories = '';
			if (isset($aCategories[$aRow['blog_id']]))
			{
				foreach ($aCategories[$aRow['blog_id']] as $aCategory)
				{
					$sCategories .= $aCategory['category_id'] . ',';
				}
			}

			$aRow['selected_categories'] = $sCategories;

			// Send the user to their new blog
			if (Phpfox::getParam('core.is_personal_site'))
			{
				$aRow['permalink'] = $this->url()->makeUrl('blog', $aRow['title_url']);
			}
			else
			{
				$aRow['permalink'] = $this->url()->makeUrl(Phpfox::getUserBy('user_name'), array('blog', $aRow['title_url']));
			}
			$aRow['permalink'] = preg_replace("/\/{$aRow['title_url']}\//i", "/<a href=\"#?type=input&amp;id=js_blog_edit_title&amp;content=js_blog_edit_inner_title&amp;call=blog.updatePermaLink&amp;blog_id={$aRow['blog_id']}\" class=\"quickEdit\" title=\"" . Phpfox::getPhrase('blog.click_edit_permalink') . "\">{$aRow['title_url']}</a>/", $aRow['permalink']);

			$bIsEdit = true;
			$this->template()->assign(array(
					'aForms' => $aRow
				)
			);

			(($sPlugin = Phpfox_Plugin::get('blog.component_controller_add_process_edit')) ? eval($sPlugin) : false);
		}
		else
		{
			Phpfox::getUserParam('blog.add_new_blog', true);
		}

		$aValidation = array(
			'title' => array(
				'def' => 'required',
				'title' => Phpfox::getPhrase('blog.fill_title_for_blog')
			),
			'text' => array(
				'def' => 'required',
				'title' => Phpfox::getPhrase('blog.add_content_to_blog')
			),
			'password' => array(
				'maxlen' => '20',
				'title' => Phpfox::getPhrase('blog.maximum_length_password', array('length' => 20))
			)
		);

		if (Phpfox::isModule('captcha') && Phpfox::getUserParam('captcha.captcha_on_blog_add'))
		{
			$aValidation['image_verification'] = Phpfox::getPhrase('captcha.complete_captcha_challenge');
		}

		(($sPlugin = Phpfox_Plugin::get('blog.component_controller_add_process_validation')) ? eval($sPlugin) : false);

		$oValid = Phpfox::getLib('validator')->set(array(
				'sFormName' => 'core_js_blog_form',
				'aParams' => $aValidation
			)
		);

		if ($aVals = $this->request()->getArray('val'))
		{
			if ($oValid->isValid($aVals))
			{
				// Add the new blog
				if (isset($aVals['publish']) || isset($aVals['draft']))
				{
					if (isset($aVals['draft']))
					{
						$aVals['post_status'] = 2;
						$sMessage = Phpfox::getPhrase('blog.blog_successfully_saved');
					}
					else
					{
						$sMessage = Phpfox::getPhrase('blog.your_blog_has_been_added');
					}

					if (($iFlood = Phpfox::getUserParam('blog.flood_control_blog')) !== 0)
					{
						$aFlood = array(
							'action' => 'last_post', // The SPAM action
							'params' => array(
								'field' => 'time_stamp', // The time stamp field
								'table' => Phpfox::getT('blog'), // Database table we plan to check
								'condition' => 'user_id = ' . Phpfox::getUserId(), // Database WHERE query
								'time_stamp' => $iFlood * 60 // Seconds);
							)
						);

						// actually check if flooding
						if (Phpfox::getLib('spam')->check($aFlood))
						{
							Phpfox_Error::set(Phpfox::getPhrase('blog.your_are_posting_a_little_too_soon') . ' ' . Phpfox::getLib('spam')->getWaitTime());
						}
					}

					if (Phpfox_Error::isPassed())
					{
						$iId = Phpfox::getService('blog.process')->add($aVals);
						$sUrlTitle = Phpfox::getService('blog')->prepareTitle($aVals['title']);
						$sUserName = Phpfox::getUserBy('user_name');
					}
				}

				// Update a blog
				if ((isset($aVals['update']) || isset($aVals['draft_update']) || isset($aVals['draft_publish'])) && isset($aRow['blog_id']) && $bIsEdit)
				{
					if (isset($aVals['draft_publish']))
					{
						$aVals['post_status'] = 1;
					}

					// Update the blog
					$iId = Phpfox::getService('blog.process')->update($aRow['blog_id'], $aRow['user_id'], $aVals, $aRow);
					$sMessage = Phpfox::getPhrase('blog.blog_updated');
					$sUrlTitle = $aRow['title_url'];
					$sUserName = $aRow['user_name'];
				}

				if (isset($iId) && $iId)
				{
					// Send the user to their new blog
					if (Phpfox::getParam('core.is_personal_site'))
					{
						$this->url()->send('blog', $sUrlTitle, $sMessage);
					}
					else
					{
						$this->url()->send($sUserName, array('blog', $sUrlTitle), $sMessage);
					}
				}
			}
		}

		$this->template()->setTitle((!empty($iEditId) ? Phpfox::getPhrase('blog.editing_blog') . ': ' . $aRow['title'] : Phpfox::getPhrase('blog.adding_a_new_blog')))
			->setBreadcrumb(Phpfox::getPhrase('blog.blogs'), $this->url()->makeUrl('blog'))
			->setBreadcrumb((!empty($iEditId) ? Phpfox::getPhrase('blog.editing_blog') . ': ' . $aRow['title'] : Phpfox::getPhrase('blog.adding_a_new_blog')), ($iEditId > 0 ? $this->url()->makeUrl('blog', array('add', 'id' => $iEditId)) : $this->url()->makeUrl('blog', array('add'))), true)
			->assign(array(
					'sCreateJs' => $oValid->createJS(),
					'sGetJsForm' => $oValid->getJsForm(),
					'bIsEdit' => $bIsEdit,
					'bCanEditPersonalData' => $bCanEditPersonalData
				)
			)
			->setEditor(array('wysiwyg' => Phpfox::getUserParam('blog.can_use_editor_on_blog')))
			->setHeader('cache', array(
				'jquery/plugin/jquery.highlightFade.js' => 'static_script',
				'switch_legend.js' => 'static_script',
				'switch_menu.js' => 'static_script',
				'quick_edit.js' => 'static_script',
				'pager.css' => 'style_css'
			)
		);

		if (Phpfox::getUserParam('attachment.can_attach_on_blog') && $bCanEditPersonalData)
		{
			$this->template()->setHeader('<script type="text/javascript">var Attachment = {sCategory: "blog", iItemId: "' . (isset($aRow['blog_id']) ? $aRow['blog_id'] : '') . '"};</script>');
		}

		(($sPlugin = Phpfox_Plugin::get('blog.component_controller_add_process')) ? eval($sPlugin) : false);
	}

	/**
	 * Garbage collector. Is executed after this class has completed
	 * its job and the template has also been displayed.
	 */
	public function clean()
	{
		(($sPlugin = Phpfox_Plugin::get('blog.component_controller_add_clean')) ? eval($sPlugin) : false);
	}
    
}
?>
